# coding=utf-8
import maya.cmds as cmds
import maya.mel as mel
import core as timelimit

class window_Record_bin ( ) :
	def textScrollList_All_List_min ( self ) :
		self.textScrollList_All_List = cmds.textScrollList ( self.Select_textScrollList , q = 1 , selectItem = 1 )

	def Delete_controller_Select ( self , *args ) :
		#self.textScrollList_All_List_min ( )
		cmds.textScrollList ( self.Select_textScrollList , e = 1 , removeItem = self.textScrollList_All_List )

	def controller_Select ( self , *args ) :
		self.starFram = int ( cmds.playbackOptions ( q = 1 , min = 1 ) )
		self.endFram = int ( cmds.playbackOptions ( q = 1 , max = 1 ) )
		self.selectNurbs = cmds.ls ( selection = 1 )
		print self.selectNurbs
		cmds.textScrollList ( self.Select_textScrollList , e = 1 , append = self.selectNurbs )

	def Get_animation ( self , *args ) :
		textScrollList_allItems = cmds.textScrollList ( self.Select_textScrollList , q = 1 , allItems = 1 )
		self.Locator_tmp = []
		for selectNurbsTmp in textScrollList_allItems :
			for Tmp in (cmds.spaceLocator ( name = selectNurbsTmp + '_tmp' )) :
				self.Locator_tmp.append ( Tmp )
				for ttt in self.Locator_tmp :
					if selectNurbsTmp in ttt :
						#mel.eval( "setAttr "+ttt+".hiddenInOutliner true;AEdagNodeCommonRefreshOutliners();")
						cmds.setAttr ( ttt + '.visibility' , 0 )
						cmds.parentConstraint ( selectNurbsTmp , ttt , w = 1 )
						print selectNurbsTmp + '11111111111111'
		cmds.bakeResults ( self.Locator_tmp , t = (self.starFram , self.endFram) , simulation = 1 , sampleBy = 1 ,
						   oversamplingRate = 1 , disableImplicitControl = 1 , preserveOutsideKeys = 1 ,
						   sparseAnimCurveBake = 0 , removeBakedAttributeFromLayer = 0 , removeBakedAnimFromLayer = 0 ,
						   bakeOnOverrideLayer = 0 , minimizeRotation = 1 , controlPoints = 0 , shape = 1 )
		for ttt in self.Locator_tmp :
			cmds.delete ( ttt , constraints = 1 )

	def Set_animation ( self , *args ) :
		textScrollList_allItems = cmds.textScrollList ( self.Select_textScrollList , q = 1 , allItems = 1 )
		for selectNurbsTmp in textScrollList_allItems :
			for ttt in self.Locator_tmp :
				if selectNurbsTmp in ttt :
					cmds.parentConstraint ( ttt , selectNurbsTmp , w = 1 )
		cmds.bakeResults ( textScrollList_allItems , t = (self.starFram , self.endFram) , simulation = 1 ,
						   sampleBy = 1 , oversamplingRate = 1 , disableImplicitControl = 1 , preserveOutsideKeys = 1 ,
						   sparseAnimCurveBake = 0 , removeBakedAttributeFromLayer = 0 , removeBakedAnimFromLayer = 0 ,
						   bakeOnOverrideLayer = 0 , minimizeRotation = 1 , controlPoints = 0 , shape = 1 )
		for selectNurbsTmp in textScrollList_allItems :
			cmds.delete ( selectNurbsTmp , constraints = 1 )
			#cmds.delete ( self.Locator_tmp )
			cmds.delete(selectNurbsTmp)
			#self.Locator_tmp.clear()

	def windows_start ( self ) :
		if timelimit.ProductActivation().Win() == 0:
			return
		windowName = 'Record_MainWindow'
		if cmds.window ( windowName , ex = True ) :
			cmds.deleteUI ( windowName )
		cmds.window ( windowName )
		cmds.paneLayout ( configuration = 'vertical2' )
		self.Select_textScrollList = cmds.textScrollList ( )
		cmds.columnLayout ( adjustableColumn = 1 )
		Controller_button = cmds.button ( label = u'添加控制器' )
		Delete_Controller_button = cmds.button ( label = u'删除选项' )
		Copybutton = cmds.button ( label = u'拷贝动画' )
		Reductionbutton = cmds.button ( label = u'还原动画' )

		cmds.button ( Controller_button , e = 1 , c = self.controller_Select )
		cmds.button ( Delete_Controller_button , e = 1 , c = self.Delete_controller_Select )
		cmds.button ( Copybutton , e = 1 , c = self.Get_animation )
		cmds.button ( Reductionbutton , e = 1 , c = self.Set_animation )

		cmds.showWindow ( windowName )


window_Record_bin ( ).windows_start ( )
